package edu.mju.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.commonutils.ReturnCode;
import edu.mju.educms.entity.CmsBanner;
import edu.mju.educms.service.CmsBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 后台管理Banner
 *
 * @author xiaoxin
 * @since 2020-09-20
 */
@RestController
@RequestMapping("/educms/admin/cms-banner")
public class BannerAdminController {

    @Autowired
    private CmsBannerService cmsBannerService;

    /**
     * 分页查询banner
     * @param current 当前页
     * @param size 页面大小
     * @return json数据
     */
    @GetMapping("/pageBanner/{current}/{size}")
    public ReturnCode pageBanner(@PathVariable Integer current,
                                 @PathVariable Integer size){
        Page<CmsBanner> page = new Page<>(current,size);
        cmsBannerService.page(page,null);
        Map<String,Object> map  = new HashMap<>();
        map.put("items",page.getRecords());
        map.put("total",page.getTotal());
        return ReturnCode.success().data(map);
    }

    /**
     * 通过id获取banner
     * @param id banner id
     * @return json数据
     */
    @GetMapping("/getBanner/{id}")
    public ReturnCode getBanner(@PathVariable String id){
        CmsBanner banner = cmsBannerService.getById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("banner",banner);
        return ReturnCode.success().data(map);
    }

    /**
     * 添加banner
     * @param cmsBanner banner对象
     * @return json数据
     */
    @PostMapping("/addBanner")
    public ReturnCode addBanner(@RequestBody CmsBanner cmsBanner){
        cmsBannerService.save(cmsBanner);
        return ReturnCode.success();
    }

    /**
     * 修改banner
     * @param cmsBanner banner对象
     * @return json数据
     */
    @PutMapping("/update")
    public ReturnCode updateById(@RequestBody CmsBanner cmsBanner){
        cmsBannerService.updateById(cmsBanner);
        return ReturnCode.success();
    }

    /**
     * 删除banner
     * @param id banner id
     * @return json数据
     */
    @DeleteMapping("/remove/{id}")
    public ReturnCode remove(@PathVariable String id){
        cmsBannerService.removeById(id);
        return ReturnCode.success();
    }
}

